package com.yowasa.xiuxian.engine;

import com.google.common.collect.Maps;
import com.yowasa.xiuxian.engine.travel.TravelInterface;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class TravelEngine implements InitializingBean {
    @Autowired
    private List<TravelInterface> travelInterfaceList;

    public static Map<Integer, TravelInterface> TRAVEL_MAP = Maps.newHashMap();


    public String exec(UserBO user) {
        Integer local = user.getLocal();
        MapEnum map = MapEnum.getById(local);
        int type = map.getType();
        TravelInterface travelInterface = TRAVEL_MAP.get(type);
        if (travelInterface == null) {
            throw new HandlerException("前方的历练还在修建,请以后再来吧");
        }
        return travelInterface.exec(user);
    }


    @Override
    public void afterPropertiesSet() {
        for (TravelInterface e : travelInterfaceList) {
            TRAVEL_MAP.put(e.getType(), e);
        }
    }
}
